// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import 'babel-polyfill'
import store from './store/index'
import router from './router'
import ViewUI from 'view-design';
import 'view-design/dist/styles/iview.css'
//引用iview自定义主题
// import  '../my-theme/index.less';

import jq from 'jquery'
import ECharts from 'vue-echarts/components/ECharts'
import echarts from "echarts";
import 'echarts/lib/chart/line';
import VueRouter from 'vue-router'
import qs from 'qs'
import apiUtil from './assets/utils/api_util'
import VueClipboard from 'vue-clipboard2'
import axios from 'axios'
import VueImageSwipe from 'vue-image-swipe'
import 'vue-image-swipe/dist/vue-image-swipe.css'
import 'bootstrap/dist/css/bootstrap.min.css'
import Vuetree from 'vue-simple-tree'
import baseUtil from './assets/utils/base-util'
import Vue2OrgTree from 'vue2-org-tree'

import '../static/UE/ueditor.config.js'
import '../static/UE/ueditor.all.js'
import '../static/UE/lang/zh-cn/zh-cn.js'
import '../static/UE/ueditor.parse.min.js'


/*video*/
import VideoPlayer from 'vue-video-player';
import 'video.js/dist/video-js.css';
import 'videojs-flash';
Vue.use(VideoPlayer)



Vue.config.productionTip = false;
Vue.prototype.$qs = qs;
Vue.prototype.$jq = jq;
Vue.prototype.$echarts = echarts;
Vue.component('chart', ECharts);


Vue.use(VueImageSwipe);
Vue.use(VueClipboard);
Vue.use(VueRouter);
// Vue.use(Vuex);
Vue.use(Vuetree);
Vue.use(ViewUI);
Vue.use(Vue2OrgTree);
Vue.use(jq);

// 授权判断
router.beforeEach((to, from, next) => {

    var pathName,
        _token,
        beforeUrl,
        para,
        params,
        isCheck,

    pathName = to.fullPath.replace('/', '');
    isCheck  =to.meta.isCheck;
    console.log('to:', to);

    if (to.fullPath == '/login') {//判断当前是否是新建的auth路由空白页面
        next();
    }  else {

        _token = sessionStorage.getItem('userId');

        beforeUrl = from.fullPath;

        // next();
        if (!_token) {//如果没有token,则让它授权
            next('/login');
        } else {

            para = {
                uid: _token,
                urlPath: pathName,
            };
            params = qs.stringify(para);

            if(isCheck == false ){
                next();
            }else {
                apiUtil.post(this, apiUtil.urls.RightCheck, params, function (res) {
                    if (res.Msg == 'deny') {
                        alert('您没有权限访问,需授权');
                        next(beforeUrl);
                    } else {
                        next();
                    }

                },function (res) {
                    next(beforeUrl);
                });

            }


            //axios的header配置

            axios.defaults.headers.post['Authorization'] = sessionStorage.getItem('token');
            axios.defaults.headers.post['Authorization'] = sessionStorage.getItem('token');
            axios.defaults.headers.get['Authorization'] = sessionStorage.getItem('token');

        }

    }

});


// let a = 3;
// router.beforeEach((to, from, next) => {
//     let userId = baseUtil.getCookie('userId');
//     if(userId){
//         next();
//     }else {
//         next('/Login');
//         console.log('跳转登录')
//     }
//
// })


// router.beforeEach((to, from, next) => {
//     if (to.path.split(':')[0] === '/http') {
//         window.open('http:' + to.path.split(':')[1], '_blank');
//         return false;
//     }
//     if (to.matched.some(r => r.meta.requireAuth)) {           //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
//         if (store.state.token) {
//             next();
//         } else {
//             next({
//                 path: '/login',
//                 query: {redirect: to.fullPath}
//             })
//         }
//     } else {
//         next();
//     }
// })
//
// const router = new VueRouter({
//     routes
// })


new Vue({
    el: '#app',
    router,
    store,
    render: h => h(App),
})
